// Zaverka3.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#include <ctype.h>
#include <malloc.h>
#define MAX 10
#define CR 13

char * GetName();
int PrnName(char *pS);

int main(int argc, char **argv)
{
	int i, number;
	char *names[10],*pt_names;
	char *res;
	int cnt;
	int fl;
	for( cnt = 0, fl = 1; cnt < MAX && fl==1; cnt++)
		{
			names[cnt] = (char*) malloc(15);
			if (names == NULL)
			{
				return NULL;
			}
			memset(names[cnt],0,sizeof(char) * 15);
			res = GetName();
			if ( res != NULL)
			{
				names[cnt] = res;
			}
			else
			{
				fl = 0;
				printf("\nMemory allocation problem\n");
			}
			pt_names = (char *) realloc(names[cnt],cnt+5);
			if (names[cnt] == NULL) 
			{
				return NULL;
			} else 
			{
				names[cnt] = pt_names; 
				pt_names = NULL; 
			}
		}
		
	
	number = cnt;
	for( cnt = 0 ; cnt < number ; cnt++)
	{
		i = PrnName(names[cnt]);
	}
	printf("\n");
	for( cnt = 0 ; cnt < number ; cnt++)
	{
		free(names[cnt]);
	}
	system("PAUSE");
	return 0;
}
char * GetName()
{
	int cnt, i;
	char *pName, *pT;
	char c;
	pName = (char *) malloc(11);
	if ( pName == NULL )
	{
		return NULL; 
	}
	memset(pName,0,sizeof(char) * 11);
	fflush(stdin);
	printf("\nName :");
	cnt = 0; 
	while(1)
	{
		for ( i = 0; i < 10 ; i++)
		{
			c = _getch();
			if ( c == CR)
			{ 
				pName[cnt] = 0;
				fflush(stdin);
				return pName;
			} else if ( isprint(c) != 0 )
			{ 
				pName[cnt++] = c;
				pName[cnt] = 0; 
				putchar(c);
			} else 
			{
				fflush(stdin); 
			}
		}
		pT = (char *) realloc(pName,cnt+10);
		if (pT == NULL) 
		{
			fflush(stdin);
			return pName;
		} else 
		{
			pName = pT; pT = NULL; 
		}
	}
	fflush(stdin);
	return pName;
}

int PrnName(char *pS)
{
	int res;
	if ( pS == NULL )
	{
	return 0;
	}
	res = printf("\nName : %s", pS);
	return 1; 
}
